package edu.cs.cs160.storytime;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class StoryTimeLibrary extends Activity{
	
	private GLSurfaceView myView;
	private StoryRenderer myRenderer;
	private TextView myTitle;
	private TextView myAuthor;
	private TextView myPages;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        
    	super.onCreate(savedInstanceState);
        
        
        //requestWindowFeature(Window.FEATURE_NO_TITLE);
        //getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        //WindowManager.LayoutParams.FLAG_FULLSCREEN);
        
        StoryTime.library.selectBook(0);
        /* un-comment this section for OpenGL
        setContentView(R.layout.library);
        myView = new GLSurfaceView(this) {
        	@Override
        	public boolean onTouchEvent(MotionEvent event) {
        		if(event.getAction() == MotionEvent.ACTION_DOWN)
        		{
	        		if(event.getX() < getWidth() / 3)
	        		{
	        			myRenderer.goLeft();
	        		}
	        		else if(event.getX() > getWidth() * 2 / 3 )
	        		{
	        			myRenderer.goRight();
	        		}
	        		else
	        		{
	        			if(myRenderer.getGeorgeLocation() == StoryRenderer.CENTER)
	        			{
	        				Intent myIntent = new Intent(this.getContext(), StoryTimeBook.class);
	        				startActivityForResult(myIntent, StoryTime.LOAD_BOOK);
	        			}
	        		}
        		}
        		
        		return true;
        	}
        };
        myRenderer = new StoryRenderer(this);
        myView.setRenderer(myRenderer);
        LinearLayout layout = (LinearLayout) findViewById(R.id.screen);
        //layout.addView(myView, layout.getChildCount());
        layout.addView(myView, layout.getChildCount());
		//*/
        
        //* NEWPART gallery addition
        //super.onCreate(savedInstanceState);
        setContentView(R.layout.library);
        myTitle = (TextView)findViewById(R.id.booktitle);
        myAuthor = (TextView)findViewById(R.id.bookauthor);
        myPages = (TextView)findViewById(R.id.bookpages);
        StoryBook tempBook = StoryTime.library.getBook(0);
		myTitle.setText(tempBook.getName());
		myAuthor.setText(tempBook.getAuthor());
		myPages.setText("Pages: " + tempBook.getNumPages());
        
		StoryTime.library.SortByTitle();
        final Gallery g = (Gallery) findViewById(R.id.gallery);
        g.setAdapter(new StoryAdapter(this));
        
        //System.out.println("or more like here? 78");
        
        g.setOnItemClickListener(new OnItemClickListener() {
        	private int oldPosition;
        	
        	@Override
            public void onItemClick(AdapterView parent, View v, int position, long id) {
        		//Toast.makeText(StoryTimeLibrary.this, "" + position, Toast.LENGTH_SHORT).show();
        		//if(position == 0 && oldPosition == 0)
        		if(position == oldPosition)
        		{
        			if (StoryTime.library.isBookEnabled(position)) {
        				StoryTime.library.selectBook(position);
        				Intent myIntent = new Intent(StoryTimeLibrary.this, StoryTimeBook.class);
        				startActivityForResult(myIntent, StoryTime.LOAD_BOOK);
        			}
        		}
        		else
        		{
        			oldPosition = position;
        			StoryBook tempBook = StoryTime.library.getBook(position);
        			myTitle.setText(tempBook.getName());
        			myAuthor.setText(tempBook.getAuthor());
        			myPages.setText("Pages: " + tempBook.getNumPages());
        		}
        		
            }

        });
        
        RadioButton sortByTitle = (RadioButton)findViewById(R.id.sbtitle);
        sortByTitle.setChecked(true);
        sortByTitle.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				StoryTime.library.SortByTitle();
				g.setAdapter(new StoryAdapter(StoryTimeLibrary.this));
				StoryBook tempBook = StoryTime.library.getBook(0);
				myTitle.setText(tempBook.getName());
				myAuthor.setText(tempBook.getAuthor());
				myPages.setText("Pages: " + tempBook.getNumPages());
			   
			}
        });
        RadioButton sortByAuthor = (RadioButton)findViewById(R.id.sbauthor);
        sortByAuthor.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				StoryTime.library.SortByAuthor();
				g.setAdapter(new StoryAdapter(StoryTimeLibrary.this));
				StoryBook tempBook = StoryTime.library.getBook(0);
				myTitle.setText(tempBook.getName());
				myAuthor.setText(tempBook.getAuthor());
				myPages.setText("Pages: " + tempBook.getNumPages());
			}
        });
        
        
        
        //*/
        //System.out.println("onCreate Finished");
    }
    
    /* un-comment this for OpenGL
    @Override
	protected void onResume() {
		super.onResume();
		myView.onResume();
	}
    
    @Override
	protected void onPause() {
		super.onPause();
		myView.onPause();
    }
    //*/

    @Override
    protected void onDestroy() {
    	myView = null;
    	myRenderer = null;
    	
    	super.onDestroy();
    	setResult(StoryTime.LOAD_BOOK_ON_EXIT);
    	Log.i("ending","ending library");
    }
    
    public class StoryAdapter extends BaseAdapter {
    	
    	private Context myContext;
    	int myBackground;
    	
    	private Integer[] myBookIds;
    	
    	public StoryAdapter(Context c)
    	{
    		myContext = c;
    		TypedArray a = obtainStyledAttributes(R.styleable.BookGallery);
    		myBackground = a.getResourceId(R.styleable.BookGallery_android_galleryItemBackground, 0);
    		//System.out.println(myBackground);
    		a.recycle();
    		//System.out.println("did it break here? 134");
    		
    		StoryLibrary tempLibrary = StoryTime.library;
    		int numBooks = tempLibrary.getBookCount();
    		myBookIds = new Integer[numBooks];
    		for(int i = 0; i < numBooks; i++)
    		{
    			myBookIds[i] = tempLibrary.getBook(i).getLibraryCover();
    		}
    		
    		
    	}


    	@Override
    	public int getCount() {
    		return myBookIds.length;
    	}

    	@Override
    	public Object getItem(int position) {
    		return position;
    	}

    	@Override
    	public long getItemId(int position) {
    		return position;
    	}

    	@Override
    	public View getView(int position, View convertView, ViewGroup parent) {
    		if(myContext == null)
    			System.out.println("really?");
    		else
    			System.out.println("good");
    		ImageView i = new ImageView(myContext);
    		
    		
    		
            i.setImageResource(myBookIds[position]);
            i.setLayoutParams(new Gallery.LayoutParams(256, 256));
            i.setScaleType(ImageView.ScaleType.FIT_XY);
            i.setBackgroundResource(myBackground);

            return i;

    	}

    }

    
}